// pages/attention/attention.js

import { queryAttentionList, cancelAttentionAction, attentionStatistics } from "../../service/attention_service"

let template = require('./../../template/tabbar.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    rightTitle: "编辑",
    myOpenId: null,
    switchArr: [
      {
        title: "新房",
        key: "",
      },
      {
        title: "二手",
        key: "",
      },
      {
        title: "商铺",
        key: "",
      },
      {
        title: "户型",
        key: "",
      },
    ],
    topTypeIndex: 0,
    houseTypeIndex: 0,
    formatTypeIndex: 0,
    houseTypeNumbers: [

    ],
    formatTypeNums: [
      {
        title: "全部",
        number: 0,
      },
      {
        title: "1室",
        number: 0,
      },
      {
        title: "2室",
        number: 0
      },
      {
        title: "3室",
        number: 0
      }
    ],
    houseList: [

    ],
    isEdit: false,//是否编辑状态
    isAllSelect: false,//是否全选
    formData: {
      page: 0,
      pageSize: 10,
      collectType: 1,
      saleStatus: "全部",
      bedNumber: "全部"
    },
    isShowLoading: false,
    isBottomShow: false,
    scrollRefreshStatus: true,
    selectArr: [],//选中进行操作的
    isLogin: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    template.tabbar("tabBar", 1, this, 0, app.globalData.roleType)//0表示第一个tabbar
    // wx.hideTabBar();
    if (app.globalData.openId) {
      this.setData({
        myOpenId: app.globalData.openId,
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.queryAttentionList();
    this.attentionStatistics();
  },
  //顶部切换
  switchAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topTypeIndex: index
    })
    // let collectType = (index == 0 ? 1 : 7);
    let collectType = 1;
    if (index == 0) {
      collectType = 1
    } else if (index == 1) {
      //二手的时候:
      collectType = 2
    } else if (index == 2) {
      collectType = 4
    }
    else if (index == 3) {
      collectType = 7
    }
    this.setData({
      'formData.page': 0,
      'formData.collectType': collectType,
      'formData.saleStatus': "全部",
      'formData.bedNumber': "全部",
      isBottomShow: false,
      selectArr: [],
      isEdit: false,
      isAllSelect: false,
      rightTitle: '编辑'
    })
    this.queryAttentionList();
    this.attentionStatistics();

  },
  //顶部类型切换
  topChangeAction(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let type = this.data.formData.collectType
    this.setData({
      houseTypeIndex: index,
      'formData.page': 0,
    })
    if (type == 1) {
      this.setData({
        'formData.saleStatus': item.name
      })
    } else if (type == 7) {
      this.setData({
        'formData.bedNumber': item.name
      })
    }
    this.queryAttentionList();

  },
  //编辑类型切换
  editChangeAction() {
    console.log("JJLog:=====")
    let houseList = this.data.houseList || [];
    if (houseList.length < 1) {
      wx.showToast({
        title: '暂无可编辑数据',
        icon: "none"
      })
      return;
    }
    this.setData({
      isEdit: !this.data.isEdit,
      rightTitle: this.data.isEdit ? '编辑' : '完成'
    })
  },


  //查询关注列表:
  queryAttentionList() {
    wx.showLoading();
    let pageNum = this.data.formData.page;
    let pageSize = this.data.formData.pageSize;
    queryAttentionList(this.data.formData).then(res => {
      wx.hideLoading();
      this.setData({
        scrollRefreshStatus: false,
      })
      console.log("JJLog:关注列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        let tempArr = [];
        tempArr = this.data.houseList;
        if (tempArr.length > 0 && pageNum > 0) {
          //加载下一页:
          tempArr = tempArr.concat(res.data);
          if (res.data.length < pageSize) {
            this.setData({
              isShowLoading: false,
              isBottomShow: true,
            })
            this.setData({
              houseList: tempArr
            })
          } else if (res.data.length == pageSize) {
            this.setData({
              isShowLoading: false,
            })
            this.setData({
              houseList: tempArr
            })
          }
        } else {
          this.setData({
            isShowLoading: false,
          })
          this.setData({
            houseList: res.data
          })
          if (res.data.length < pageSize) {
            this.setData({
              isBottomShow: true
            })
          }
        }
        if (this.data.houseNumber && this.data.houseList.length > 0) {
          this.dealHouseList();
        }
      }
    }).catch(err => {
      this.setData({
        scrollRefreshStatus: false,
      })
      let page = this.data.formData.page;
      if (page > 0) {
        page = page - 1;
      }
      this.setData({
        isShowLoading: false,
        'formData.page': page,
      })
      wx.hideLoading();
      if (!app.globalData.isLogin) {
        wx.showToast({
          title: "暂无数据",
          icon: "none"
        })
      } else {
        wx.showToast({
          title: err || "暂无数据",
          icon: "none"
        })
      }

      console.error("JJLog:queryAttentionList", err);
    })
  },
  dealHouseList() {
    let arr = this.data.houseList;
    arr.forEach((item) => {
      item.isChoose = false
    })
    this.setData({
      houseList: arr
    })
  },
  //查询关注统计
  attentionStatistics() {
    attentionStatistics(this.data.formData.collectType).then(res => {
      console.log("JJLog:统计信息是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          houseTypeNumbers: res.data || [],
          houseTypeIndex: 0,
        })
      }
    }).catch(err => {
      console.error("JJLog:attentionStatistics", err);
    })
  },

  //取消关注点击;
  cancelAttention() {
    let arr = this.data.selectArr;
    if (arr.length < 1) {
      wx.showToast({
        title: (this.data.formData.collectType != 7) ? "请至少选中一个房源" : "请至少选中一个户型",
        icon: "none"
      })
      return;
    }
    this.cancelAttentionAction();
  },
  //取消关注列表:
  cancelAttentionAction() {
    wx.showLoading();
    cancelAttentionAction(this.data.selectArr).then(res => {
      wx.hideLoading();
      console.log("JJLog:取消关注成功:" + JSON.stringify(res));
      if (res.code == 200) {
        wx.showToast({
          title: '操作成功',
          icon: "none"
        })
        this.setData({
          'formData.page': 0,
          isBottomShow: false,
          isEdit: false,
          selectArr: [],
          rightTitle: this.data.isEdit ? '编辑' : '完成'
        })
        this.queryAttentionList();
        this.attentionStatistics();
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:cancelAttentionAction", err);
    })
  },

  //单项选择:
  singleSelectItem(e) {
    let index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    let isChoose = !item.isChoose
    //动态数组:
    let changeItem = 'houseList[' + index + '].isChoose';
    this.setData({
      [changeItem]: !item.isChoose,
    })


    //1.处理加入或者从selectArr中删除,
    if (isChoose) {
      //加入数组中:
      let sArr = this.data.selectArr;
      let obj = {
        collectType: this.data.formData.collectType,
        number: "",
        openId: this.data.myOpenId
      }
      if (this.data.formData.collectType == 1 || this.data.formData.collectType == 2) {
        obj.number = item.houseNumber;
      } else if (this.data.formData.collectType == 4) {
        obj.number = item.shopNumber
      }
      else if (this.data.formData.collectType == 7) {
        obj.number = item.formatNumber
      }
      sArr.push(obj);
      this.setData({
        selectArr: sArr
      })
    } else {
      let cArr = this.data.selectArr;
      let cIndex = 0;
      cArr.forEach((cItem, idx) => {
        if (this.data.formData.collectType == 1 && item.houseNumber == cItem.number) {
          cIndex = idx;
        }
        if (this.data.formData.collectType == 7 && item.formatNumber == cItem.number) {
          cIndex = idx;
        }
      })
      cArr.splice(cIndex, 1);
      this.setData({
        selectArr: cArr
      })

    }
    console.log("JJLog:选中arr:" + JSON.stringify(this.data.selectArr));



    //2.处理是否是全选:
    if (this.data.selectArr.length == this.data.houseList.length) {
      this.setData({
        isAllSelect: true
      })
    } else {
      this.setData({
        isAllSelect: false
      })
    }
  },

  // 全选操作:
  allSelectAction() {
    let isChoose = !this.data.isAllSelect;
    if (isChoose) {
      let sArr = [];
      let list = this.data.houseList;
      let type = this.data.formData.collectType;
      list.forEach((item, index) => {
        let obj = {
          collectType: type,
          openId: this.data.myOpenId,
        }
        if (type == 1 || type == 2) {
          obj.number = item.houseNumber
        }
        if (type == 4) {
          obj.number = item.shopNumber;
        }
        if (type == 7) {
          obj.number = item.formatNumber
        }
        item.isChoose = true
        sArr.push(obj)
      })
      this.setData({
        selectArr: sArr,
        houseList: list,
        isAllSelect: true
      })
    } else {
      let tList = this.data.houseList;
      tList.forEach((tItem) => {
        tItem.isChoose = false
      })
      this.setData({
        isAllSelect: false,
        selectArr: [],
        houseList: tList
      })
    }
  },


  //scroll滚动到底了:
  scrolltolower() {
    if (this.data.isBottomShow) {
      return;
    }
    let tempPage = this.data.formData.page + 1;
    this.setData({
      'formData.page': tempPage,
      isShowLoading: true,
      selectArr: [],
      isEdit: false,
      isAllSelect: false,
      rightTitle: this.data.isEdit ? '编辑' : '完成'
    })
    this.queryAttentionList();
  },

  //scroll下拉刷新了:
  refreshDownData() {

    this.setData({
      'formData.page': 0,
      isBottomShow: false,
      selectArr: [],
      isEdit: false,
      isAllSelect: false,
      rightTitle: this.data.isEdit ? '编辑' : '完成'
    })
    this.queryAttentionList();
    this.attentionStatistics();

  },
  //跳转到对应详情:
  pushToHouseDetail(e) {
    let item = e.currentTarget.dataset.item;
    if (this.data.formData.collectType == 1) {
      //房源详情:
      wx.navigateTo({
        url: '/newHousePackage/pages/newHouseDetail/newHouseDetail?houseNumber=' + item.houseNumber,
      })

    } else if (this.data.formData.collectType == 2) {
      //二手详情;
      wx.navigateTo({
        url: '/secondHousePackage/pages/secondHouseDetail/secondHouseDetail?houseNumber=' + item.houseNumber,
      })
    } else if (this.data.formData.collectType == 4) {
      //商铺详情:
      wx.navigateTo({
        url: '/secondHousePackage/pages/shopModule/shopHouseDetail/shopHouseDetail?shopNumber=' + item.shopNumber,
      })
    }
    else if (this.data.formData.collectType == 7) {
      //户型详情:
      wx.navigateTo({
        url: '/newHousePackage/pages/houseTypeDetail/houseTypeDetail?formatNumber=' + item.formatNumber,
      })
    }
  },



})